package velox.jcr.mapper;

import javax.jcr.RepositoryException;


public abstract class CachingLoader<T> implements Loader<T> {

	public String getKey(String uuid) {
		return getClass().getSimpleName() + "#" + uuid;
	}
	
	public abstract T doLoad(String uuid) throws RepositoryException;
	
	@SuppressWarnings("unchecked")
	public T load(String uuid) throws RepositoryException {
		SecondLevelCache cache = UnitOfWorkHolder.get().getSecondLevelCache();
		
		String key = getKey(uuid);
		
		T t = (T) cache.get(key, CacheType.AUX);
		if (t != null) return t;

		t = doLoad(uuid);
		cache.put(key, t, CacheType.AUX);
		return t;
	}

}
